package com.ydlclass.config;

import com.ydlclass.compress.Compress;
import com.ydlclass.compress.CompressFactory;
import com.ydlclass.discovery.Registry;
import com.ydlclass.loadbalancer.LoadBalance;
import com.ydlclass.serialize.Serialize;
import com.ydlclass.serialize.SerializerFactory;
import com.ydlclass.spi.SpiHandler;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import java.util.List;

/**
 * spi服务发现
 * @Author duWenJian
 * @Date 2024/3/4 13:05
 * @Version 1.0
 */
@Slf4j
public class SpiResolver {
    /**
     * 通过spi的方式来加载配置
     * @param configuration 配置上下文
     */
    public void loadFromSpi(Configuration configuration) {

        // 我的spi文件中配置了很多实现(自由定义)
        ObjectWrapper<LoadBalance> loadBalanceObjectWrapper = SpiHandler.get(LoadBalance.class);
        if (loadBalanceObjectWrapper != null){
            configuration.setLoadBalance(loadBalanceObjectWrapper.getImpl());
        }

        ObjectWrapper<Serialize> serializeWrapper = SpiHandler.get(Serialize.class);
        if (serializeWrapper != null){
            SerializerFactory.addSSerialize(serializeWrapper);
        }

        ObjectWrapper<Compress> CompressWrapper = SpiHandler.get(Compress.class);
        if (CompressWrapper != null){
            CompressFactory.addCompress(CompressWrapper);
        }
    }
}
